package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.utils.CacheUtil;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice/")
@Api(tags = "03.公告管理模块")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping("list")
    @ApiOperation("首页公告展示")
    @ApiOperationSupport(order = 10)
    public JsonResult list() {
        Serializable noticeIndex = cacheUtil.get("notice");
        List<NoticeListVO> noticeListVOList = null;
        if (noticeIndex == null) {
            noticeListVOList = noticeMapper.noticeList();
            cacheUtil.setExpire("notice", (Serializable) noticeListVOList, 30);
            return JsonResult.ok(noticeListVOList);
        }
        return JsonResult.ok(noticeIndex);
    }


//    @Autowired
//    private RedisTemplate<String, Serializable> redisTemplate;
//
//
//    @GetMapping("list")
//    @ApiOperation("首页公告展示")
//    @ApiOperationSupport(order = 10)
//    public JsonResult list() {
//        HashOperations<String, String, Serializable> ops = redisTemplate.opsForHash();
//        if (ops.entries("notice") == null || (ops.entries("notice").isEmpty())) {
//            List<NoticeListVO> noticeListVOList = noticeMapper.noticeList();
//            for (NoticeListVO noticeListVO : noticeListVOList) {
//                ops.put("notice",String.valueOf(noticeListVO.getId()),noticeListVO);
//            }
//        }
//        return JsonResult.ok(ops.entries("notice").values());
//    }

}
